package de.baumann.browser.activitys.oc

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import de.baumann.browser.R
import de.baumann.browser.activitys.gg.GMainActivity
import de.baumann.browser.adapter.PagerAdapter
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.db.ETHWallet
import de.baumann.browser.fragments.OcAllFragment
import de.baumann.browser.fragments.OcFailedFragment
import de.baumann.browser.fragments.OcInFragment
import de.baumann.browser.fragments.OcOutFragment
import de.baumann.browser.iview.IBaseView
import de.baumann.browser.present.WalletDetailPresenter
import de.baumann.browser.utils.getETHWallet
import de.baumann.browser.utils.getMoneyAddress
import de.baumann.browser.utils.getUser
import de.baumann.browser.utils.saveETHWallet
import de.baumann.browser.utils.web3j.BalanceTask
import de.baumann.browser.utils.web3j.OdinWallet
import de.baumann.browser.utils.web3j.WalletDaoUtils
import kotlinx.android.synthetic.main.activity_oc_wallet_detail.*
import kotlinx.android.synthetic.main.include_tool_bar.*

class OcWalletDetailActivity : BaseOdinActivity<IBaseView, BasePresenter<IBaseView>>() {

    companion object {
        fun startOcWalletDetailActivity(context: Context) {
            val user = getUser()
            var ethWallet: ETHWallet? = null
            if (user != null) {
                var address = user.eth_address
                if (address != null) {
                    if (address.startsWith("odx")) {
                        address = "0${address.substring(2)}"
                    }
                    ethWallet = WalletDaoUtils.queryWalletByAddress(address)
                }
            }

            if (ethWallet == null) {
                ethWallet = WalletDaoUtils.loadAll()[0]
            }

            context.startActivity(Intent(context, OcWalletDetailActivity::class.java).apply {

                val bundel = Bundle().apply {
                    putSerializable("odinWallet", ethWallet)
                }
                putExtras(bundel)
            })
        }

        fun startOcWalletDetailActivity(context: Context, ethWallet: ETHWallet) {
            context.startActivity(Intent(context, OcWalletDetailActivity::class.java).apply {
                val bundel = Bundle().apply {
                    putSerializable("odinWallet", ethWallet)
                }
                putExtras(bundel)
            })
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_gg, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        GMainActivity.startGMainActivity(mContext, getWallet())
//        presenter?.getSmelteryData()
        return super.onOptionsItemSelected(item)
    }

    override fun getLayout(): Int {
        OdinWallet.removeAllActivity()
        OdinWallet.addAtivity(this)
        return R.layout.activity_oc_wallet_detail
    }

    override fun getTitleText(): String {
        return "ODIN 钱包"
    }

    override fun initView() {
        initToolbar(commToolBar, commTitle)
        val bundle = Bundle()
        bundle.putString("address", getWallet().address)
        bundle.putString("cn", "ODIN")

        val all = OcAllFragment.instance
        val out = OcOutFragment.instance
        val intr = OcInFragment.instance
        val fail = OcFailedFragment.instance

        all.arguments = bundle
        out.arguments = bundle
        intr.arguments = bundle
        fail.arguments = bundle

        val fragmentList = arrayListOf(all, out, intr, fail)

        val titles = arrayOf("全部", "转出", "转入", "失败")
        val adapter = PagerAdapter(supportFragmentManager, fragmentList, titles)
        viewPagerOc.adapter = adapter
        tabLayoutOc.setupWithViewPager(viewPagerOc)

        tvOdinCollectMoney.setOnClickListener {
            CollectMoneyActivity.startCollectMoneyActivity(mContext, getWallet().address)
        }

        tvOdinTransfer.setOnClickListener {
            OcTransferActivity.startOcTransferActivity(mContext, getWallet())
        }

        ivWalletList.setOnClickListener {
            OcWalletSettingActivity.startOcWalletSettingActivityForResult(this, getWallet())
        }
    }

    override fun initData() {
//        tvOcDetailWalletName.text = getWallet().name
//        BalanceTask(tvOcDetailWalletBalance).execute(getWallet().address)
        BalanceTask(tvOcDetailWalletBalance).execute(getMoneyAddress())
    }


    override fun onResume() {
        super.onResume()
//        tvOcDetailWalletName.text = WalletDaoUtils.getWalletName(getWallet().getAddress())
        tvOcDetailWalletName.text = WalletDaoUtils.getWalletName(getMoneyAddress())
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            finish()
        }
    }


    private fun getWallet(): ETHWallet {
        return intent.getSerializableExtra("odinWallet") as ETHWallet
    }

    override fun onDestroy() {
        super.onDestroy()
        OdinWallet.removeActivity(this)
    }


    fun getAddress(): String {
        return getWallet().address
    }


    override fun showBindEth(): Boolean {
        return false
    }

    override fun createPresenter(): BasePresenter<IBaseView>? {
        return null
    }

    override fun createView(): IBaseView? {
        return null
    }

}
